package sisicotren.ui.windows;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDialog;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;

import sisicotren.model.simulator.SimulationStats;

public class SimulationResult extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JTable tablaResultado = null;
	private JButton jButton = null;
	private JPanel bottomPanel;  //  @jve:decl-index=0:
	private List<SimulationStats> simulationStats = null;
	private Object [][] data = null;
	private String [] cols = null;
	private int maxQueueSizeInitIndex;
	private int maxTimeInQueuesInitIndex;
	private int totalClientsInitIndex;
	private int totalSimulationTimeIndex;
	private int lambdaIndex;
	private int muIndex;
	private JButton jButton1 = null;
	private MainWindow owner = null;
	private JButton jButton2 = null;
	/**
	 * @param owner
	 */
	public SimulationResult(MainWindow owner, List<SimulationStats> stats) {
		super(owner.getJFrame());
		this.owner = owner;
		this.simulationStats = stats;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Resultado de la Simulacion");
		this.setContentPane(getJContentPane());
		this.setLocationByPlatform(true);
		this.pack();
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			JScrollPane scroll = new JScrollPane(this.getResultTable());
			jContentPane.add(scroll, BorderLayout.CENTER);
			jContentPane.add(getBottomPanel(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}

	private void printTableDescription() {
		// se imprimen los rotulos de las columnas.
		if(data == null) {
			
			int maxQueueSizes = 0;
			int maxTimeQueueSizes = 0;
			for(int i=0; i < this.simulationStats.size(); i++) {
				if(this.simulationStats.get(i).getMaxQueueSizes().size() > maxQueueSizes)
					maxQueueSizes = this.simulationStats.get(i).getMaxQueueSizes().size();
				
				if(this.simulationStats.get(i).getMaxTimeInQueue().size() > maxTimeQueueSizes)
					maxTimeQueueSizes = this.simulationStats.get(i).getMaxTimeInQueue().size();
			}
			
			data = new Object[2 + maxQueueSizes + maxTimeQueueSizes + 2][this.simulationStats.size() + 1];
			maxQueueSizeInitIndex = 0;
			for(int i=0; i < maxQueueSizes; i++)
				data[maxQueueSizeInitIndex + i][0] = "Max. Tamaño Cola " + (i + 1);
			maxTimeInQueuesInitIndex = maxQueueSizes;
			for(int j=0; j < maxTimeQueueSizes; j++)
				data[maxTimeInQueuesInitIndex + j][0] = "Max. Tiempo Cliente en Cola (mins) " + (j + 1);
			
			totalClientsInitIndex = maxTimeInQueuesInitIndex + maxTimeQueueSizes;
			data[totalClientsInitIndex][0] = "Cantidad total de Clientes ";
			totalSimulationTimeIndex = totalClientsInitIndex + 1; 
			data[totalSimulationTimeIndex][0] = "Tiempo total de simulacion ";
			
			lambdaIndex = totalSimulationTimeIndex + 1; 
			data[lambdaIndex][0] = "Arribos (cli/min)";
			
			muIndex = lambdaIndex + 1; 
			data[muIndex][0] = "Capacidad Molinete (cli/min)";
			
		}
	}
	
	private JTable getResultTable() {
		if(this.tablaResultado == null) {
						
			Iterator<SimulationStats> it = this.simulationStats.iterator();
			cols = new String[this.simulationStats.size() + 1];
			cols[0] = "Estadistica";
			
			this.printTableDescription();
			
			int currStat = 1;
			while(it.hasNext()) {
				
				cols[currStat] = "Corrida " + currStat;
				
				SimulationStats curr = it.next();
				ArrayList<Integer> maxQueueSizes = curr.getMaxQueueSizes();
				ArrayList<Double> maxTimeInQueues = curr.getMaxTimeInQueue();
	
				for(int i=0; i < maxQueueSizes.size(); i++)
					data[maxQueueSizeInitIndex + i][currStat] = maxQueueSizes.get(i);
				
				for(int j=0; j < maxTimeInQueues.size(); j++)
					data[maxTimeInQueuesInitIndex + j][currStat] = maxTimeInQueues.get(j);
				
				data[totalClientsInitIndex][currStat] = curr.getTotalClients();
				
				long horas = TimeUnit.HOURS.convert(curr.getTotalSimulationTime(), TimeUnit.MILLISECONDS);
				long mins = TimeUnit.MINUTES.convert(curr.getTotalSimulationTime(), TimeUnit.MILLISECONDS);
				long segs = TimeUnit.SECONDS.convert(curr.getTotalSimulationTime(), TimeUnit.MILLISECONDS);
				String info = "" + (horas < 10 ? "0" + horas : horas) + ":" + 
					(mins % 60 < 10 ? "0" + mins % 60 : mins % 60 ) + ":" +
					(segs % 60 < 10 ? "0" + segs % 60 : segs % 60);
				
				data[totalSimulationTimeIndex][currStat] = info;
				
				data[lambdaIndex][currStat] = curr.getLambdaUsed();
				data[muIndex][currStat] = curr.getMuUsed();
				
				currStat++;
			}
			this.tablaResultado = new JTable( 
					new ReadOnlyAbstractTableModel(cols, data));
			this.tablaResultado.setFillsViewportHeight(true);
		}
		
		return this.tablaResultado;
	}

	public JPanel getBottomPanel() {
		if(this.bottomPanel == null) {
			this.bottomPanel = new JPanel();
			this.bottomPanel.add(this.getJButton());
			bottomPanel.add(getJButton1(), null);
			bottomPanel.add(getJButton2(), null);
		}
		
		return this.bottomPanel;
	}
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton("Aceptar");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton("Limpiar");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					owner.cleanReportStaticsList();
					owner.setStatusOfOpenReportButton(false);
					dispose();
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("Ver grafico");
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					SimulationResultGraphic graph = new SimulationResultGraphic(owner.getJFrame(), simulationStats);
					graph.setVisible(true);
					
				}
			});
		}
		return jButton2;
	}
	
}

